package com.bookstore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookstore.entity.dto.BookDTO;
import com.bookstore.entity.po.AuthorPo;
import com.bookstore.entity.po.BookAuthorPo;
import com.bookstore.entity.po.BookClassOP;
import com.bookstore.entity.po.BookPO;
import com.bookstore.entity.pojo.R;
import com.bookstore.entity.vo.BookVO;
import com.bookstore.service.AuthorService;
import com.bookstore.service.BookAuthorService;
import com.bookstore.service.BookClassService;
import com.bookstore.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("book")
@CrossOrigin
public class BookController {

    static final  int publisher = -100;
    static final  int author = -99;

    @Autowired
    BookService bookService;
    @Autowired
    AuthorService authorService;
    @Autowired
    BookAuthorService bookAuthorService;
    @Autowired
    BookClassService bookClassService;
    @GetMapping
    public R getAllBooks(Integer start,Integer end) {
        QueryWrapper<BookPO> queryWrapper = new QueryWrapper<>();
        List<BookPO> list = bookService.list();
        List<BookPO> collect = list.stream().filter(v -> v.getStatus().equals(0)).collect(Collectors.toList());
        List<BookPO> bookPOS = collect.subList(start >= collect.size()?collect.size():start , end > collect.size()? collect.size() :end);
        return R.success(bookPOS);
    }

    @GetMapping("selectOne")
    public R selectOneById(Integer id) {
        List<BookAuthorPo> bookAuthorPos = bookAuthorService.list(new QueryWrapper<BookAuthorPo>().eq("book_id", id));
        List<Integer> authorIdList = bookAuthorPos.stream().map(BookAuthorPo::getAuthorId).collect(Collectors.toList());
        List<AuthorPo> authorPos = authorService.list();
        List<AuthorPo> collect = authorPos.stream().filter(x -> authorIdList.contains(x.getId()))
                .collect(Collectors.toList());


        BookPO bookPO = bookService.getById(id);
        BookClassOP bookClassOP = bookClassService.getById(bookPO.getClassId());
        bookPO.setAuthors(collect);
        bookPO.setBookClassName(bookClassOP.getClassName());
        bookPO.setInfor(bookPO.getInfor().replaceAll("\n","<br />"));
        return R.success(bookPO);
    }
    @PostMapping("search")
    public R search (@RequestBody BookDTO bookDTO) {
        QueryWrapper<BookPO> qw = new QueryWrapper<>();
        if(bookDTO.getBookClass() >= 0){
            if (bookDTO.getBookClass() != 0)
                qw.eq("class_id",bookDTO.getBookClass());
            qw.eq("status",0);
            qw.like("book_name",bookDTO.getBookName());

            List<BookPO> list = bookService.list(qw);
            List<BookVO> list1 = new ArrayList<>();
            for (BookPO bookPO : list) {
                list1.add(new BookVO(bookPO.getId(),bookPO.getBookName()));
            }
            return R.success(list1);
        }else if (bookDTO.getBookClass().equals(publisher)){
            //qw.eq("class_id",bookDTO.getBookClass());
            qw.eq("status",0);
            qw.like("publisher",bookDTO.getBookName());

            qw.groupBy("publisher");
            List<BookPO> list = bookService.list(qw);
            List<BookVO> list1 = new ArrayList<>();
            for (BookPO bookPO : list) {
                list1.add(new BookVO(bookPO.getId(), bookPO.getPublisher()));
            }
            return R.success(list1);
        }else {
            QueryWrapper<AuthorPo> authorPoQueryWrapper = new QueryWrapper<>();
            authorPoQueryWrapper.like("name",bookDTO.getBookName());
            List<AuthorPo> list = authorService.list(authorPoQueryWrapper);
            List<BookVO> list1 = new ArrayList<>();
            list.forEach(authorPo -> {
                list1.add(new BookVO(authorPo.getId(),authorPo.getName()));
            });
            return R.success(list1);
        }

    }
    @PostMapping("find")
    public R find (@RequestBody BookDTO bookDTO) {
        QueryWrapper<BookPO> qw = new QueryWrapper<>();

        if(bookDTO.getBookClass() >= 0){

            if (bookDTO.getBookClass() != 0)
                qw.eq("class_id",bookDTO.getBookClass());
            if (bookDTO.getBookName() != null && !bookDTO.getBookName().equals("")) {
                qw.like("book_name",bookDTO.getBookName());
            }
            qw.eq("status",0);

            List<BookPO> list = bookService.list(qw);
            return R.success(list);
        }else if(bookDTO.getBookClass().equals(publisher)){

            if (bookDTO.getBookName() != null && !bookDTO.getBookName().equals("")) {
                qw.like("publisher",bookDTO.getBookName());
            }

            List<BookPO> list = bookService.list(qw);
            return R.success(list);
        }else {
            QueryWrapper<BookAuthorPo> bookAuthorPoQueryWrapper = new QueryWrapper<>();
            QueryWrapper<AuthorPo> authorPoQueryWrapper = new QueryWrapper<>();
            authorPoQueryWrapper.eq("name",bookDTO.getBookName());
            AuthorPo one = authorService.getOne(authorPoQueryWrapper);
            bookAuthorPoQueryWrapper.eq("auth_id",one.getId());
            List<BookAuthorPo> list = bookAuthorService.list(bookAuthorPoQueryWrapper);
            List<Integer> idList = list.stream().map(BookAuthorPo::getBookId).collect(Collectors.toList());

            QueryWrapper<BookPO> bookPOQueryWrapper = new QueryWrapper<>();

            bookAuthorPoQueryWrapper.eq("id",idList);


            List<BookPO> list1 = bookService.list();
            List<BookPO> collect = list1.stream().filter(x -> idList.contains(x.getId())).collect(Collectors.toList());
            return R.success(collect);
        }

    }
}
